package com.vito.oa.act.extend.service.impl;

import static com.vito.oa.act.constant.OaActContants.CACHE_ACT_VALUE;
import static com.vito.oa.act.extend.model.OaActExBussModel.CACHE_KEY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.act.model.ActExBussModel;
import com.vito.act.service.impl.ActExBussServiceImpl;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.base.util.JSONUtil;
import com.vito.base.util.StringUtil;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.constant.OaActContants.ProcessButtonEnum;
import com.vito.oa.act.extend.dao.OaActExBussDefMapper;
import com.vito.oa.act.extend.dao.OaActExBussMapper;
import com.vito.oa.act.extend.dao.OaActExProcessBranchMapper;
import com.vito.oa.act.extend.model.OaActEExBussDefModel;
import com.vito.oa.act.extend.model.OaActExBussDefModel;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.extend.service.OaActExBussService;

@Service(group="OaActExBussServiceImpl")
public class OaActExBussServiceImpl extends ActExBussServiceImpl implements OaActExBussService  {
	
	@Autowired
	private OaActExBussMapper actExBussMapper;
	@Autowired
	private OaActExBussDefMapper actExBussDefMapper;
	@Autowired
	private OaActExProcessBranchMapper branchMapper;
	
	
	@Override
	@CacheEvict(value=CACHE_ACT_VALUE,allEntries=true)
	public int saveActExBuss(OaActExBussModel actExBussModel,List json) {
		actExBussModel.setBusinessId(StringUtil.getUUID());
		//保存流程的同时保存流程节点信息
		if(json==null||json.size()==0){
			return 0;
		}
		for (int i = 0; i < json.size(); i++) {
			if(i==0){
				OaActExBussDefModel start = new OaActExBussDefModel();
				start.setProcessKey(actExBussModel.getProcessKey());
				start.setProcessNodeId(OaActContants.FORM_PROCESS_START);
				start.setProcessNodeName(OaActContants.FORM_PROCESS_STARTNAME);
				start.setOrdernum(i);
				actExBussDefMapper.saveActExBussDef(start);
				actExBussDefMapper.saveActEExBussDef(start);
			}
			Map obj = (Map)json.get(i); 
			OaActExBussDefModel model = new OaActExBussDefModel();
			model.setProcessKey(actExBussModel.getProcessKey());
			model.setProcessNodeId(obj.get("key")+"");
			Map name = (Map)obj.get("nameExpression");
			Map description = (Map)obj.get("descriptionExpression");
			if(description != null){
				Map documentation = JSONUtil.convertString2Map((String)description.get("expressionText"));
				if(documentation.containsKey("maxApprover")){
					model.setMaxApprover((int)documentation.get("maxApprover"));
				}
				if(documentation.containsKey("isSequential")){
					model.setIsSequential("true".equals((String)documentation.get("isSequential"))?"1":"0");
				}
			}
			model.setProcessNodeName(name.get("expressionText")+"");
			model.setOrdernum(i+1);
			actExBussDefMapper.saveActExBussDef(model);
			actExBussDefMapper.saveActEExBussDef(model);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dept_id", actExBussModel.getCreateDept());
		map.put("business_id", actExBussModel.getBusinessId());
		map.put("operuser", actExBussModel.getCreateUser());
		actExBussMapper.saveActExBuss(actExBussModel);
		return actExBussMapper.saveActEExBuss(actExBussModel);
	}

	@Override
	@CacheEvict(value=CACHE_ACT_VALUE,allEntries=true)
	public int updateActExBuss(OaActExBussModel actExBussModel) {
		actExBussMapper.updateActEExBuss(actExBussModel);
		return super.updateActExBuss(actExBussModel);
	}

	@Override
	@CacheEvict(value=CACHE_ACT_VALUE,allEntries=true)
	public int deleteActExBuss(ActExBussModel actExBussModel) {
		actExBussMapper.deleteActEExBuss(actExBussModel);
		actExBussMapper.deleteActEExBussDef(actExBussModel);
		return super.deleteActExBuss(actExBussModel);
	}

	@Override
	@Cacheable(value = CACHE_ACT_VALUE, key = CACHE_KEY)
	public DataGrid<ActExBussModel> queryActExBuss(OaActExBussModel actExBussModel) {
		DataGrid<ActExBussModel> data = new DataGrid<ActExBussModel>();
			data.setRows(actExBussMapper.queryActExBussByPage(actExBussModel));
			data.setTotal(actExBussModel.getTotal());
		return data;
	}

	@Override
	public DataGrid<OaActEExBussDefModel> queryActBussDef(OaActEExBussDefModel actExBussModel) {
		DataGrid<OaActEExBussDefModel> data = new DataGrid<OaActEExBussDefModel>();
		List<OaActEExBussDefModel> listdata = actExBussMapper.queryActBussDef(actExBussModel);
		if(null == listdata || listdata.isEmpty()){
			return null;
		}
		for(OaActEExBussDefModel defModel : listdata){
			if(defModel.getApprovalId() == null ){
				continue;
			}
			String userids[] = defModel.getApprovalId().split(OaActContants.SEPARATOR);
			List<Map<String,String>> userIdList = new ArrayList<Map<String,String>>();
			for(String id :userids){
				Map<String,String> userIdMap = new HashMap<String,String>();
				userIdMap.put("user_id", id);
				userIdMap.put("user_type", defModel.getApprovalTypeCode());
				userIdList.add(userIdMap);
			}
			List<MyHashMap<String, String>> result = branchMapper.trUserInfo(userIdList);
			String useridStr ="";
			String name =null;
			Iterator<MyHashMap<String, String>> it = result.iterator();
			while(it.hasNext()){
				MyHashMap<String, String> resultData = it.next();
				name = resultData.get("username");
				if(Validata.notNullOrEmpty(name)){
					useridStr += name;
				}
			}
			if(Validata.notNullOrEmpty(useridStr)){
				defModel.setApprovalId(useridStr);	
			}else{
				defModel.setApprovalId(null);
			}
		}
		data.setTotal(listdata.size());
		data.setRows(listdata);
		return data;
	}
}
